package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.YearRate;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

/*
        晒收益
 */
public class SaishouyiActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mShengqianBack;
    /**
     * 39.8
     */
    private TextView mTextView17;
    /**
     * 15652760856
     */
    private TextView mTvName;
    private ImageView mMyTouxinag;
    private ImageView mMyErweima;
    /**
     * 查看好友助力
     */
    private TextView mTvZhuli;
    /**
     * 晒晒我的年化收益
     */
    private Button mButShaishai;
    private ImageView mGvmineBack;
    private ImageView mImager;
    private YearRate yearRate;
    private PopupWindow window;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saishouyi);
        initView();
        getnetshouyi();


    }



    private void getnetshouyi() {

        String url = ApiManager.YearRate + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this);
        Log.e("shaishouyi", "主题url====" + url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {



            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                yearRate = new Gson().fromJson(responseInfo.result, YearRate.class);
               if (yearRate.getCode().equals("10000")){

                   Glide.with(SaishouyiActivity.this).load(yearRate.getData()).into(mImager);

               }else {
                   Toast.makeText(SaishouyiActivity.this, yearRate.getMsg(),Toast.LENGTH_SHORT).show();
               }



            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(SaishouyiActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_shaishai:
                //分享
                View view1 =   LayoutInflater.from(SaishouyiActivity.this).inflate(R.layout.popwindownshangtan,null, false);
                window = new PopupWindow(view1, RelativeLayout.LayoutParams.MATCH_PARENT,600,true );
                window.setOutsideTouchable(true);
                window.setTouchable(true);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 0.7f;
                getWindow().setAttributes(lp);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1f;
                        getWindow().setAttributes(lp);
                    }
                });
                /*
                        分享朋友
                 */
                view1.findViewById(R.id.share_penyou).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        UMImage image = new UMImage(SaishouyiActivity.this, yearRate.getData());//网络图片
                        image.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图
                        image.compressStyle = UMImage.CompressStyle.QUALITY;//质量压缩，适合长图的分享
                        new ShareAction(SaishouyiActivity.this).setPlatform(SHARE_MEDIA.WEIXIN).withMedia(image).setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {
                                window.dismiss();
                                Toast.makeText(SaishouyiActivity.this, " 分享成功", Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {

                            }
                        }).share();



                    }
                });
                /*
                        分享朋友圈
                 */
                view1.findViewById(R.id.share_pengyouquna).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UMImage image = new UMImage(SaishouyiActivity.this, yearRate.getData());//网络图片
                        image.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图
                        image.compressStyle = UMImage.CompressStyle.QUALITY;//质量压缩，适合长图的分享
                        new ShareAction(SaishouyiActivity.this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).withMedia(image).setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {
                                window.dismiss();
                                Toast.makeText(SaishouyiActivity.this, " 分享成功", Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {

                            }
                        }).share();

                    }
                });
                //关闭弹窗
                view1.findViewById(R.id.imgguanbi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                    }
                });
                window.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM , 0, 0);


                break;
            case R.id.Gvmine_back:
                finish();
                break;
            case R.id.tv_zhuli:
                Intent intent = new Intent(SaishouyiActivity.this,ZhuliActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void initView() {
        mGvmineBack = (ImageView) findViewById(R.id.Gvmine_back);
        mGvmineBack.setOnClickListener(this);
        mImager = (ImageView) findViewById(R.id.imager);
        mTvZhuli = (TextView) findViewById(R.id.tv_zhuli);
        mTvZhuli.setOnClickListener(this);
        mButShaishai = (Button) findViewById(R.id.but_shaishai);
        mButShaishai.setOnClickListener(this);
    }


}
